//地图
(function() {

    function Map(width, height) {
        this.width = width || 600;
        this.height = height || 600;
    }
    window.Map = Map;
}());

let mapObj = new Map();
let dvArr=[];
//格子
(function() {
    let map = document.querySelector("#map");

    function Lattice(width, height) {
        this.width = width || 40;
        this.height = height || 40;
        this.piece = new Piece();
    }
    window.Lattice = Lattice;
    window.rocl=new Array(15);
    Lattice.prototype.draw = function() {
        let count = (mapObj.width / this.width) * (mapObj.height / this.height);
        for (let i = 0; i < (mapObj.width / this.width); i++) {
            rocl[i]=new Array(15);
            for(let j=0;j<(mapObj.height / this.height);j++){
                rocl[i][j]=0;
                let dv = document.createElement("div");
                dv.setAttribute("index",[i,j]);
                dv.style.width = this.width + "px";
                dv.style.height = this.height + "px";
                dv.style.border = "1px solid #333333";
                dv.addEventListener("click", this.piece.draw)
                map.append(dv);
            }

        }
    }

}());


//棋子
(function() {
    window.blackPiece=[];
    window.whitePiece=[];
    window.rowCol=null;
    function Piece() {
    }
    window.Piece = Piece;
    let piece=new Piece();
    Piece.prototype.black="img/timg.jpg";
    Piece.prototype.white="img/bai.jpg";
    //画在地图上
    Piece.prototype.draw = function(e) {
        //获得坐标
        // let x=Math.floor(e.clientX/40);
        // let y=Math.floor(e.clientY/40);
        rowCol=null;
        if(game.player=="black"){
            if(!this.style.backgroundImage){
                this.style.backgroundImage="url("+piece.black+")";
                rowCol=(this.getAttribute("index").split(","));
                rocl[parseInt(rowCol[0])][parseInt(rowCol[1])]=2;//黑
                game.player="white";
            }
        }else{
            if(!this.style.backgroundImage) {
                this.style.backgroundImage = "url(" + piece.white + ")";
                rowCol=(this.getAttribute("index").split(","));
                rocl[parseInt(rowCol[0])][parseInt(rowCol[1])]=1;//白
                game.player = "black";
            }
        }

        game.judgeGame();


    }
}());






//棋手
(function() {
    function ChessPlayer(name) {
        this.name = name;
        this.piece=new Piece();
    }

    window.ChessPlayer = ChessPlayer;
    ChessPlayer.prototype.play = function() {

    }
}());



//Game
(function() {
    function Game() {
        this.map = new Map();//地图
        this.lattice=new Lattice();
        this.blacker = new ChessPlayer();//黑棋手
        this.Whiteer=new ChessPlayer();//白棋手
        // this.piece=new Piece();

        //正在下棋玩家
        this.player=null;
    }

    window.Game = Game;
    let game=new Game();

    //初始化游戏
    Game.prototype.init = function() {
        this.lattice.draw();
        this.player="black";
    }

    //开始游戏
    Game.prototype.play=function () {
        this.init();

    }

    Game.prototype.judgeGame=function () {
        //获得是黑还是白在下棋
        let t=this.player=="black"?1:2;
        //获得当前下棋的坐标
        let row=parseInt(rowCol[0]);
        let col=parseInt(rowCol[1]);
        let count=1;

        //右边
        while (col-1>0&&rocl[row][col-1]==t){
            count++;
            col--;
        }
        row=parseInt(rowCol[0]);
        col=parseInt(rowCol[1]);
        //左边
        while (col+1<15&&rocl[row][col+1]==t){
            col++;
            count++;
        }
        if(count>=5){
            t==1?alert("白棋赢"):alert("黑棋赢");
        }

        //向上
        row=parseInt(rowCol[0]);
        col=parseInt(rowCol[1]);
        count=1;
        while(row-1>0&&rocl[row-1][col]==t)
        {
            row--;
            count++;
        }
        row=parseInt(rowCol[0]);
        col=parseInt(rowCol[1]);
        //向下
        while(row+1<15&&rocl[row+1][col]==t)
        {
            row++;
            count++;
        }
        if(count>=5){
            t==1?alert("白棋赢"):alert("黑棋赢");
        }


        //左下
        row=parseInt(rowCol[0]);
        col=parseInt(rowCol[1]);
        count=1;
        while(row+1<15&&col-1>0&&rocl[row+1][col-1]==t)
        {
            row++;
            col--;
            count++;
        }
        //右上
        row=parseInt(rowCol[0]);
        col=parseInt(rowCol[1]);
        while(row-1>0&&col+1<15&&rocl[row-1][col+1]==t)
        {
            row--;
            col++;
            count++;
        }
        if(count>=5){
            t==1?alert("白棋赢"):alert("黑棋赢");
        }


        //左上
        row=parseInt(rowCol[0]);
        col=parseInt(rowCol[1]);
        count=1;
        while(row-1>0&&col-1>0&&rocl[row-1][col-1]==t)
        {
            row--;
            col--;
            count++;
        }
        //右下
        row=parseInt(rowCol[0]);
        col=parseInt(rowCol[1]);
        while(row+1<15&&col+1<15&&rocl[row+1][col+1]==t)
        {
            row++;
            col++;
            count++;
        }
        if(count>=5){
            t==1?alert("白棋赢"):alert("黑棋赢");
        }








    }








}());